package org.parklab.lab.web.client;

import java.util.Date;

import org.parklab.lab.web.client.data.user.UserInfo;
import org.parklab.lab.web.client.rpcservice.LoginService;
import org.parklab.lab.web.client.rpcservice.LoginServiceAsync;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class KeepTyping implements EntryPoint {
	VerticalPanel layoutPanel = new VerticalPanel();
	TextBox myTextField = new TextBox();
	FlexTable wholeTextLines = new FlexTable();
	
	
	String chatKey = "chat123";
	
	int lastRow = 0;
	
	private RTCServiceAsync rtSvc = GWT.create(RTCService.class);
	

	private void cw(String msg) {
		System.out.println((new Date()).toString() + "\t" + msg);
		wholeTextLines.setText(lastRow + 2, 0, (new Date()).toString());
		wholeTextLines.setText(lastRow + 2, 1, msg); lastRow++;
	}
	
	private UserInfo userInfo = null;
	private VerticalPanel loginPanel = new VerticalPanel();
	private Label loginLabel = new Label("로그인해 주세요.");
	private Anchor signInLink = new Anchor("로그인");
	private Anchor signOutLink = new Anchor("로그아웃");
	
	
	
	public void onModuleLoad() {
		// Check login status using login service.
		LoginServiceAsync loginSvc = GWT.create(LoginService.class);
		loginSvc.login(GWT.getHostPageBaseURL(), new AsyncCallback<UserInfo>() {
			
			@Override
			public void onSuccess(UserInfo result) {
				userInfo = result;
				if (null != result) {
					cw("out, userinfo: " + result.toString());
				} else {
					cw("out, userinfo is null");
				}
				if (userInfo.isLoggedIn()) {
					loadMainUI();
				} else {
					loadLogin();
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// do nothing
			}
		});
		
	}


	private void loadLogin() {
		// Assemble login panel
		signInLink.setHref(userInfo.getLoginUrl());
		loginPanel.add(loginLabel);
		loginPanel.add(signInLink);
		RootPanel.get("wholeContainer").add(loginPanel);
	}

	private void loadMainUI() {
		signOutLink.setHref(userInfo.getLogoutUrl());
		
		layoutPanel.add(wholeTextLines);
		layoutPanel.add(myTextField);
		layoutPanel.add(signOutLink);
		
		myTextField.setStyleName("mytextfield");
		wholeTextLines.setStyleName("wholetextfield");
		layoutPanel.setStyleName("layoutPanel");
		
		RootPanel.get("wholeContainer").add(layoutPanel);
	
		myTextField.addKeyDownHandler(new KeyDownHandler() {
			
			@Override
			public void onKeyDown(KeyDownEvent event) {
				/*
				if (rtSvc == null) {
					rtSvc = GWT.create(RTCService.class);
				}

				RTCDB myMessage = new RTCDB(userInfo.getUserId(), myTextField.getText(), chatKey);
				
				cw("before send " + myMessage.toString());
				rtSvc.sendRTC(myMessage, new AsyncCallback<RTCDB>() {
					
					@Override
					public void onSuccess(RTCDB result) {
						cw(result.toString() + "is successfully sent.");
					}
					

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}
				})
				;
				*/
			}
		});	
		
		refreshRT();
		myTextField.setFocus(true);
	}

	private void refreshRT() {
		if (rtSvc == null) {
			rtSvc = GWT.create(RTCService.class);
		}
		
//		AsyncCallback<RTCDB>	callback = new AsyncCallback<RTCDB>() {
//
//			@Override
//			public void onFailure(Throwable caught) {
//				// TODO Auto-generated method stub
//				
//			}
//
//			@Override
//			public void onSuccess(RTCDB result) {
//				if (result!=null) {
//				wholeTextLines.setText(0, 0, result.getUserKey());
//				wholeTextLines.setText(0, 1, result.getText());
//				} else {
//					wholeTextLines.setText(0, 0, (new Date()).toString());
//					if (wholeTextLines.getText(0, 1) == "" ||
//							wholeTextLines.getText(0, 1) == "Asking...") {
//						wholeTextLines.setText(0, 1, "No one is typing.");
//					}
//				}
//				refreshRT();
//			}
//		};
//		
//		wholeTextLines.setText(0, 0, (new Date()).toString());
//		wholeTextLines.setText(0, 1, "Asking...");
//		rtSvc.getRTC(userInfo.getUserId(), chatKey, callback);
	}
}
